﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Web.Wap.Code;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Web.ZLOAuth.Code;

namespace ZLCMS.Web.ZLOAuth
{
    public partial class GetMemberCode : MemberPageBase
    {
        protected string GotoUrl { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            string code = Guid.NewGuid().ToString("N");
            using(ISession session = SessionFactoryHelper.CreateSession())
            {
                //把之前的记录设置为已使用
                session.CreateSQLQuery("update member_oauth_code set status='1' where MEMBER_ACCOUNT=:acc")
                    .SetParameter("acc", CurrentMemberAccount)
                    .ExecuteUpdate();


                session.CreateSQLQuery("INSERT INTO member_oauth_code(USER_CODE,OAUTH_CODE,MEMBER_ACCOUNT,STATUS,RECORD_DATE) VALUES(:u,:c,:acc,'0',:d);")
                    .SetParameter("u", OAuthServerConfig.UserCode)
                    .SetParameter("c", code)
                    .SetParameter("acc", CurrentMemberAccount)
                    .SetParameter("d", DateTime.Now)
                    .ExecuteUpdate();
            }
            GotoUrl = Request["goto"];
            if (GotoUrl.IndexOf("?") > 0)
            {
                GotoUrl += "&";
            }
            else
            {
                GotoUrl += "?";
            }
            GotoUrl += "membercode=" + code;
            //Response.Redirect(gotoUrl);

        }
    }
}